/*******************************************************************************
 * File name: 		main.c
 * Description:
 * Project: 		_framework
 * Target: 			LPC1788
 * Compiler: 		arm-none-eabi-gcc
 * Date: 			Jun 26, 2012
 * Author: 			kuba
 * Based on: 		http://www.freddiechopin.info/
 ******************************************************************************/
/*==============================================================================
 Includes test
==============================================================================*/
# include <stddef.h>
# include <stdint.h>
# include <string.h>
# include "LPC177x_8x.h"
# include "system_LPC177x_8x.h"
# include "init.h"
# include "board.h"
# include "timer.h"
# include "gpdma.h"



/*==============================================================================
 Globals
==============================================================================*/
static struct io led0, led1, led2, switch0;
static uint8 src[] = {0x11, 0x22, 0x33, 0x44, 0x55};
static uint8 dst[sizeof(src)];
/*==============================================================================
 Static function prototypes
==============================================================================*/
static void jtagDelay   	(void);
static void gpdmaService 		(void);
static void errorService	(void);
/*==============================================================================
 Main function
==============================================================================*/
int main(void)
{
	jtagDelay();
	systemInit();
	//SystemInit();

	/* peripheral init */
	led0 = boardLedInit(gpioPORT0, 25);
	led1 = boardLedInit(gpioPORT0, 26);
	led2 = boardLedInit(gpioPORT0, 23);
	switch0 = boardSwitchInit(gpioPORT0, 29);
	/* GPDMA */
	struct gpdmaConf mygpdma;
	gpdmaChannel mychannel;
	gpdmaInit(irqLOWEST_PRIORITY);
	mygpdma.mode            = gpdmaM2M;
	mygpdma.srcWidth        = gpdmaS_WIDTH8;
	mygpdma.dstWidth        = gpdmaD_WIDTH8;
	mygpdma.sbSize          = gpdmaSB_SIZE4;
	mygpdma.dbSize          = gpdmaDB_SIZE4;
	mygpdma.srcPeriph       = 0;
	mygpdma.dstPeriph       = 0;
	mygpdma.flowCtrl        = gpdmaFLOW_CTRL_M2M_DMA;
	mygpdma.srcAddr         = (uint32)src;
	mygpdma.dstAddr         = (uint32)dst;
	mygpdma.transferEndService = gpdmaService;
	mygpdma.errorService = errorService;
	mychannel = gpdmaChnl7Init(&mygpdma);

	volatile uint32 i;
	while (1)
	{
		if (boardSwitchPressed(switch0))
		{
			gpdmaChnl7Enable(mychannel, sizeof(src));
		}
		for (i = 0; i < 1000000; ++i);
		boardLedOn(led1);
		for (i = 0; i < 1000000; ++i);
		boardLedOff(led1);
	}
}

/*==============================================================================
 Static function definitions
==============================================================================*/
/*------------------------------------------------------------------------------
 function name:   gpdmaService
 description:     led on at the end of GPDMA transfer
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void gpdmaService (void)
{
	if (memcmp(src, dst, sizeof(src)) == 0)
	{
		memset(dst, 0x00, sizeof(src));
		boardLedOn(led0);
	}
}

/*------------------------------------------------------------------------------
 function name:  errorService
 description:    led on if any errors during GPDMA transfer
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void errorService	(void)
{
	boardLedToggle(led2);
}
/*------------------------------------------------------------------------------
 function name:   jtagDelay
 description:     function provides a delay required by jtag
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void jtagDelay (void)
{
   volatile uint32 i;
   for (i = 0; i < 1000000; ++i);
}
/******************************************************************************
* END OF FILE
******************************************************************************/
